﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using UMC.Web.UI;
using UMC.Web;
using UMC.Wiki.Entities;
using System.Collections;

namespace UMC.Wiki.Activities
{
    class SubjectCodeActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var sId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
            {
                return this.DialogValue("none");

            }), 0);
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {

                this.Prompt("请登录", false);
                this.Context.Send("Login", true);

            }
            var Code = this.AsyncDialog("Code", "auto");


            var sub = DataFactory.Instance().Subject(sId) ?? new Subject { Id = sId, Code = Code };
            if (String.Equals(sub.Code, Code, StringComparison.CurrentCultureIgnoreCase))
            {
                return;
            }

            var member = DataFactory.Instance().ProjectMember(sub.project_id.Value, user.Id.Value);

            if (member != null)
            {
                switch (member.AuthType)
                {
                    case WebAuthType.Admin:
                    case WebAuthType.User:
                        break;
                    default:
                        this.Prompt("您未有编辑此图文的权限");
                        break;
                }

            }
            else
            {
                this.Prompt("您未有编辑此图文的权限");
            }
            //}

            if (DataFactory.Instance().Subject(sub.project_item_id.Value, Code) != null)
            {
                this.Prompt("此简码已存在");
            }

            DataFactory.Instance().Put(new Subject { Code = Code, Id = sub.Id });
            var project = DataFactory.Instance().Project(sub.project_id.Value);
            var projectItem = DataFactory.Instance().ProjectItem(sub.project_item_id.Value);
            this.Context.Send(new UMC.Web.WebMeta().Put("type", "Subject.Change").Put("id", sub.Id).Put("Sub", sub.Id).Put("text", sub.Title).Put("code", Code)
                .Put("path", String.Format("{0}/{1}/{2}", project.Code, projectItem.Code, Code)), true);



        }

    }
}